/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIVentanaP.java
 *
 * Created on 15-ene-2010, 13:52:52
 */

package GUI;

import compilador.AnalizadorLex;
import compilador.GestorErrores;
import compilador.Traductor;
import compilador.datos.Token;
import java.awt.Color;
import java.awt.Cursor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import util.TXTFilter;

/**
 *
 * @author usuario
 */
public class GUIVentanaPrincipal extends javax.swing.JFrame {

  //  private static final String RUTA_RECURSOS = "/recursos/";
 //   private ImageIcon _iconoVentana = new ImageIcon("recursos/ferrita.png");
    private ImageIcon _iconoVentana = new ImageIcon(getClass().getResource("/recursos/ferrita.png"));
    private File archivo;
    private FileReader fr;
    //¿Hacen falta realmente estas dos variables de arriba como atributos?
    private BufferedReader br;
    /* atributo "buff" que permitirá saber al analizador léxico cuando se ha llegado
     * al fin de fichero
     */
    private FileWriter fw;
    private BufferedWriter bw;
    private int buff;
    private GestorErrores _gestErr;
    private AnalizadorLex lex;
    private Traductor trad;
    private MaquinaVirtualThread th;
    private Monitor mon;
    private String ruta;
    private File file;

    /** Creates new form GUIVentanaP */
    public GUIVentanaPrincipal() {
        ruta =System.getProperty( "java.class.path" );
        ruta=ruta.substring(0,ruta.length()-14 );
        initComponents();
        mon=new Monitor();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _panelEditor = new javax.swing.JPanel();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        _btnLimpiar = new javax.swing.JButton();
        _scrPanePrograma = new javax.swing.JScrollPane();
        _txtAPrograma = new javax.swing.JTextArea();
        _panelOutput = new javax.swing.JPanel();
        jSeparator4 = new javax.swing.JSeparator();
        _pestañaOutput = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        _txtAreaCodigoMaquina = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        _txtAreaOutput = new javax.swing.JTextArea();
        //this._txtAreaOutput.getDocument().addDocumentListener( this );
        jScrollPane3 = new javax.swing.JScrollPane();
        _panelTraza = new javax.swing.JPanel();
        _labelInstrucTraza = new javax.swing.JLabel();
        _txtInstruccion = new javax.swing.JTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        _tablePila = new javax.swing.JTable();
        _labelPila = new javax.swing.JLabel();
        _txtFieldCimaPila = new javax.swing.JTextField();
        _labelCimaPila = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        _tablaMemDatos = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        _btnAvanzar = new javax.swing.JButton();
        _btnStopTraza = new javax.swing.JButton();
        _botonera = new javax.swing.JToolBar();
        _btnCargarPrograma = new javax.swing.JButton();
        _btnGuardarPrograma = new javax.swing.JButton();
        _btnCompilar = new javax.swing.JButton();
        _btnEjecutar = new javax.swing.JButton();
        _btnTrazar = new javax.swing.JButton();
        _barraProgreso = new javax.swing.JProgressBar();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Compilador");
        setExtendedState(MAXIMIZED_BOTH);
        setIconImage(this._iconoVentana.getImage());
        setResizable(false);

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        _btnLimpiar.setText("Limpiar");
        _btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnLimpiarActionPerformed(evt);
            }
        });

        _scrPanePrograma.setBackground(new java.awt.Color(255, 255, 255));

        _txtAPrograma.setColumns(20);
        _txtAPrograma.setRows(5);
        _scrPanePrograma.setViewportView(_txtAPrograma);

        javax.swing.GroupLayout _panelEditorLayout = new javax.swing.GroupLayout(_panelEditor);
        _panelEditor.setLayout(_panelEditorLayout);
        _panelEditorLayout.setHorizontalGroup(
            _panelEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, _panelEditorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(_scrPanePrograma, javax.swing.GroupLayout.DEFAULT_SIZE, 817, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_btnLimpiar)
                .addGap(73, 73, 73))
            .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 979, Short.MAX_VALUE)
        );
        _panelEditorLayout.setVerticalGroup(
            _panelEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(_panelEditorLayout.createSequentialGroup()
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(_panelEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_scrPanePrograma, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addGroup(_panelEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                        .addComponent(_btnLimpiar)))
                .addGap(3, 3, 3))
        );

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);

        _txtAreaCodigoMaquina.setColumns(20);
        _txtAreaCodigoMaquina.setEditable(false);
        _txtAreaCodigoMaquina.setRows(5);
        jScrollPane1.setViewportView(_txtAreaCodigoMaquina);

        _pestañaOutput.addTab("Codigo MaquinaP", jScrollPane1);

        _txtAreaOutput.setColumns(20);
        _txtAreaOutput.setRows(5);
        _txtAreaOutput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                _txtAreaOutputKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(_txtAreaOutput);

        _pestañaOutput.addTab("Output", jScrollPane2);

        _labelInstrucTraza.setText("Instruccion a ejecutar:");

        _txtInstruccion.setEditable(false);

        _tablePila.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Pila"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(_tablePila);

        _labelPila.setText(" Pila:");

        _txtFieldCimaPila.setEditable(false);

        _labelCimaPila.setText("Cima Pila:");

        _tablaMemDatos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Memoria Datos"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(_tablaMemDatos);

        jLabel1.setText("Memoria Datos:");

        javax.swing.GroupLayout _panelTrazaLayout = new javax.swing.GroupLayout(_panelTraza);
        _panelTraza.setLayout(_panelTrazaLayout);
        _panelTrazaLayout.setHorizontalGroup(
            _panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(_panelTrazaLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(_panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_txtInstruccion, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_labelInstrucTraza))
                .addGap(26, 26, 26)
                .addGroup(_panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_labelPila)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(_panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_txtFieldCimaPila, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_labelCimaPila))
                .addGap(46, 46, 46)
                .addGroup(_panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        _panelTrazaLayout.setVerticalGroup(
            _panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(_panelTrazaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(_panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_labelInstrucTraza)
                    .addComponent(jLabel1)
                    .addComponent(_labelCimaPila)
                    .addComponent(_labelPila))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(_panelTrazaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(_txtInstruccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_txtFieldCimaPila, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane5, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(68, Short.MAX_VALUE))
        );

        jScrollPane3.setViewportView(_panelTraza);

        _pestañaOutput.addTab("Debug", jScrollPane3);

        _btnAvanzar.setText("Avanzar");
        _btnAvanzar.setVisible(false);
        _btnAvanzar.setEnabled(false);
        _btnAvanzar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnAvanzarActionPerformed(evt);
            }
        });

        _btnStopTraza.setText("Stop");
        _btnStopTraza.setVisible(false);
        _btnStopTraza.setEnabled(false);
        _btnStopTraza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnStopTrazaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout _panelOutputLayout = new javax.swing.GroupLayout(_panelOutput);
        _panelOutput.setLayout(_panelOutputLayout);
        _panelOutputLayout.setHorizontalGroup(
            _panelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(_panelOutputLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(_pestañaOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 814, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(_panelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(_btnStopTraza, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(_btnAvanzar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        _panelOutputLayout.setVerticalGroup(
            _panelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
            .addGroup(_panelOutputLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(_btnAvanzar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(_btnStopTraza)
                .addContainerGap(202, Short.MAX_VALUE))
            .addGroup(_panelOutputLayout.createSequentialGroup()
                .addComponent(_pestañaOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                .addContainerGap())
        );

        _botonera.setRollover(true);

        _btnCargarPrograma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/abrirCarpeta.png"))); // NOI18N
        _btnCargarPrograma.setText("Cargar Programa");
        _btnCargarPrograma.setFocusable(false);
        _btnCargarPrograma.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        _btnCargarPrograma.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        _btnCargarPrograma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnCargarProgramaActionPerformed(evt);
            }
        });
        _botonera.add(_btnCargarPrograma);

        _btnGuardarPrograma.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/guardar.png"))); // NOI18N
        _btnGuardarPrograma.setText("Guardar Programa");
        _btnGuardarPrograma.setFocusable(false);
        _btnGuardarPrograma.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        _btnGuardarPrograma.setMaximumSize(new java.awt.Dimension(89, 85));
        _btnGuardarPrograma.setMinimumSize(new java.awt.Dimension(89, 85));
        _btnGuardarPrograma.setPreferredSize(new java.awt.Dimension(89, 85));
        _btnGuardarPrograma.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        _btnGuardarPrograma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnGuardarProgramaActionPerformed(evt);
            }
        });
        _botonera.add(_btnGuardarPrograma);

        _btnCompilar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/compilar.png"))); // NOI18N
        _btnCompilar.setText("Compilar");
        _btnCompilar.setFocusable(false);
        _btnCompilar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        _btnCompilar.setMaximumSize(new java.awt.Dimension(89, 85));
        _btnCompilar.setMinimumSize(new java.awt.Dimension(89, 85));
        _btnCompilar.setPreferredSize(new java.awt.Dimension(89, 85));
        _btnCompilar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        _btnCompilar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnCompilarActionPerformed(evt);
            }
        });
        _botonera.add(_btnCompilar);

        _btnEjecutar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/btnConectar.png"))); // NOI18N
        _btnEjecutar.setText("Ejecutar");
        _btnEjecutar.setFocusable(false);
        _btnEjecutar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        _btnEjecutar.setMaximumSize(new java.awt.Dimension(89, 85));
        _btnEjecutar.setMinimumSize(new java.awt.Dimension(89, 85));
        _btnEjecutar.setPreferredSize(new java.awt.Dimension(89, 85));
        _btnEjecutar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        _btnEjecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnEjecutarActionPerformed(evt);
            }
        });
        _botonera.add(_btnEjecutar);

        _btnTrazar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/Trazar.jpg"))); // NOI18N
        _btnTrazar.setText("Trazar");
        _btnTrazar.setFocusable(false);
        _btnTrazar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        _btnTrazar.setMaximumSize(new java.awt.Dimension(89, 85));
        _btnTrazar.setMinimumSize(new java.awt.Dimension(89, 85));
        _btnTrazar.setPreferredSize(new java.awt.Dimension(89, 85));
        _btnTrazar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        _btnTrazar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnTrazarActionPerformed(evt);
            }
        });
        _botonera.add(_btnTrazar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 979, Short.MAX_VALUE)
            .addComponent(_panelEditor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(_barraProgreso, javax.swing.GroupLayout.DEFAULT_SIZE, 979, Short.MAX_VALUE)
            .addComponent(_botonera, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 979, Short.MAX_VALUE)
            .addComponent(_panelOutput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(_botonera, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_panelEditor, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_panelOutput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_barraProgreso, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void _txtAreaOutputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event__txtAreaOutputKeyPressed

    
      
    if(evt.getKeyCode()==java.awt.event.KeyEvent.VK_ENTER)
    {
            
       
       
                mon.put();
        
    }
}//GEN-LAST:event__txtAreaOutputKeyPressed

private void _btnTrazarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnTrazarActionPerformed
    
   // _btnAvanzar.setVisible(true);
    Object[] lista= new Object[6] ;
    lista[0]=_txtInstruccion;
    lista[1]=_tablePila;
    lista[2]=_txtFieldCimaPila;
    lista[3]=_tablaMemDatos;
    lista[4]=_btnAvanzar;
    lista[5]=_btnStopTraza;
    _btnAvanzar.setEnabled(true);
    _btnAvanzar.setVisible(true);
    _btnStopTraza.setEnabled(true);
    _btnStopTraza.setVisible(true);
    _pestañaOutput.setSelectedIndex(2);
    th= new MaquinaVirtualThread(_txtAreaOutput,ruta+"archivo.obj",mon,true,lista);
    th.start();
   // _btnAvanzar.setVisible(false);
}//GEN-LAST:event__btnTrazarActionPerformed

private void _btnAvanzarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnAvanzarActionPerformed

    
    this.mon.put();
}//GEN-LAST:event__btnAvanzarActionPerformed

private void _btnCargarProgramaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnCargarProgramaActionPerformed
// TODO add your handling code here:JFileChooser chooser = new JFileChooser();
       JFileChooser chooser = new JFileChooser();
       chooser.setCurrentDirectory(new java.io.File("."));
       chooser.setDialogTitle("Cargar archivo");
       TXTFilter filter= new TXTFilter("Txt", "txt");
       chooser.setFileFilter(filter);
       if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            archivo = chooser.getSelectedFile();
            try {
                this.fr = new FileReader(this.archivo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(GUIVentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.br = new BufferedReader(this.fr);
            this._txtAPrograma.setText("");
            escribirPanel(br,_txtAPrograma);
        }

}//GEN-LAST:event__btnCargarProgramaActionPerformed

private void _btnGuardarProgramaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnGuardarProgramaActionPerformed
      
       JFileChooser chooser = new JFileChooser();
       chooser.setCurrentDirectory(new java.io.File("."));
       chooser.setDialogTitle("Guardar archivo");
       TXTFilter filter= new TXTFilter("Txt", "txt");
       chooser.setFileFilter(filter);
       if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            archivo = chooser.getSelectedFile();
            try {
                this.fw = new FileWriter(this.archivo);
                this.bw = new BufferedWriter(this.fw);
                bw.write(this._txtAPrograma.getText());
                bw.close();
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(GUIVentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }

            

        }
}//GEN-LAST:event__btnGuardarProgramaActionPerformed

private void _btnCompilarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnCompilarActionPerformed
            StringReader rd= new StringReader(this._txtAPrograma.getText());
            BufferedReader brd=new BufferedReader(rd);
                      
           
            _gestErr = new GestorErrores();
            //Paso al A. Lexico el nombre del fichero fuente
            lex = new AnalizadorLex(brd, false,_gestErr);
          //  StringWriter sw=new StringWriter(); 
           // BufferedWriter bwt=new BufferedWriter(sw);
            //Paso al Traductor el nombre del fichero objeto
            trad = new Traductor(lex, ruta+"archivo.obj", false,_gestErr);
            //Lanzo el Analizador Lexico
            lex.scanner();
            //Lanzo el Traductor despues el Analizador LExico.
            trad.parser();
            if(_gestErr.getErrorres().size()>0)
            {
                if(_gestErr.getErrorres().get(0).startsWith("Traducción finalizada con éxito"))
                {
                    try {
                        this.fr = new FileReader(ruta+"archivo.obj");
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(GUIVentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    this.br = new BufferedReader(this.fr);
                    this._txtAreaCodigoMaquina.setText("");
                    escribirPanel(br,_txtAreaCodigoMaquina);
                    _pestañaOutput.setSelectedIndex(0);
                    
                }
                else
                {
                this._txtAreaCodigoMaquina.setText("");
                for(int i=0;i<_gestErr.getErrorres().size();i++ )
                {
                    this._txtAreaCodigoMaquina.append(_gestErr.getErrorres().get(i)+"\n");
                }}
            }
           
            
}//GEN-LAST:event__btnCompilarActionPerformed

private void _btnEjecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnEjecutarActionPerformed
    _txtAreaOutput.setText("");
    _pestañaOutput.setSelectedIndex(1);
    th= new MaquinaVirtualThread(_txtAreaOutput,ruta+"archivo.obj",mon,false);
    th.start();
}//GEN-LAST:event__btnEjecutarActionPerformed

private void _btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnLimpiarActionPerformed
_txtAPrograma.setText("");
}//GEN-LAST:event__btnLimpiarActionPerformed

private void _btnStopTrazaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnStopTrazaActionPerformed
    th.parar();
    this.mon.put();
}//GEN-LAST:event__btnStopTrazaActionPerformed

    private void escribirPanel(BufferedReader bfr,JTextArea txt)
    {
        String linea=null;
        int numLinea=1;
        try {
            linea=bfr.readLine();
            while (linea != null) {
                txt.append(" "+linea+"\n");
                linea=bfr.readLine();
                numLinea++;
            }
            bfr.close();
        } catch (IOException ex) {
            Logger.getLogger(GUIVentanaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar _barraProgreso;
    private javax.swing.JToolBar _botonera;
    private javax.swing.JButton _btnAvanzar;
    private javax.swing.JButton _btnCargarPrograma;
    private javax.swing.JButton _btnCompilar;
    private javax.swing.JButton _btnEjecutar;
    private javax.swing.JButton _btnGuardarPrograma;
    private javax.swing.JButton _btnLimpiar;
    private javax.swing.JButton _btnStopTraza;
    private javax.swing.JButton _btnTrazar;
    private javax.swing.JLabel _labelCimaPila;
    private javax.swing.JLabel _labelInstrucTraza;
    private javax.swing.JLabel _labelPila;
    private javax.swing.JPanel _panelEditor;
    private javax.swing.JPanel _panelOutput;
    private javax.swing.JPanel _panelTraza;
    private javax.swing.JTabbedPane _pestañaOutput;
    private javax.swing.JScrollPane _scrPanePrograma;
    private javax.swing.JTable _tablaMemDatos;
    private javax.swing.JTable _tablePila;
    private javax.swing.JTextArea _txtAPrograma;
    private javax.swing.JTextArea _txtAreaCodigoMaquina;
    private javax.swing.JTextArea _txtAreaOutput;
    private javax.swing.JTextField _txtFieldCimaPila;
    private javax.swing.JTextField _txtInstruccion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    // End of variables declaration//GEN-END:variables

}
